<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!doctype html>
<html class="no-js" lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>购物车 || Rongcha - 抹茶</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Place favicon.ico in the root directory -->
    <link rel="shortcut icon" type="image/x-icon" href="<%=basePath%>images/img/favicon.ico">
    <!-- All CSS Hear -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <link rel="stylesheet" href="<%=basePath%>css/global.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <link rel="stylesheet" href="<%=basePath%>css/bootstrap.min.css" />
    <link rel="stylesheet" href="<%=basePath%>css/simple-line-icons.css" />
    <link rel="stylesheet" href="<%=basePath%>css/icofont.css" />
    <link rel="stylesheet" href="<%=basePath%>css/magnific-popup.css" />
    <link rel="stylesheet" href="<%=basePath%>css/animate.css" />
    <link rel="stylesheet" href="<%=basePath%>css/nice-select.css" />
    <link rel="stylesheet" href="<%=basePath%>css/jquery-ui.css" />
    <link rel="stylesheet" href="<%=basePath%>css/owl.carousel.min.css" />
    <link rel="stylesheet" href="<%=basePath%>css/mainmenu.css" />
    <!-- Style CSS Hear -->
    <link rel="stylesheet" href="<%=basePath%>css/style.css" />
    <link rel="stylesheet" href="<%=basePath%>css/responsive.css" />
    <script src="<%=basePath%>js/vendor/modernizr-2.8.3.min.js"></script>

    <style>
        :root {
            --primary-color: #8B9E6B;
            --text-primary: #333;
            --text-regular: #666;
            --text-secondary: #999;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f8f9fa;
            margin: 0;
            padding: 0;
        }

        .wrapper {
            max-width: 1400px;
            margin: 0 auto;
            padding: 20px;
            background: #f8f9fa;
            min-height: 100vh;
        }

        .cart-header {
            background: linear-gradient(135deg, var(--primary-color), #7a8d5d);
            color: white;
            padding: 40px 0;
            text-align: center;
            margin-bottom: 30px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(139, 158, 107, 0.3);
        }

        .cart-header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            font-weight: 600;
        }

        .cart-header p {
            font-size: 1.1rem;
            opacity: 0.9;
            margin: 0;
        }

        .cart-content {
            display: grid;
            grid-template-columns: 1fr 350px;
            gap: 30px;
            margin-bottom: 30px;
        }

        .cart-main {
            background: white;
            border-radius: 12px;
            padding: 30px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.08);
        }

        .cart-sidebar {
            background: white;
            border-radius: 12px;
            padding: 25px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.08);
            height: fit-content;
            position: sticky;
            top: 20px;
        }

        .cart-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 0;
        }

        .cart-table th,
        .cart-table td {
            padding: 20px 15px;
            text-align: left;
            border-bottom: 1px solid #e1e5e9;
            vertical-align: middle;
        }

        .cart-table th {
            background: #f8f9fa;
            font-weight: 600;
            color: var(--text-primary);
            font-size: 14px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .cart-table tbody tr {
            transition: all 0.3s;
        }

        .cart-table tbody tr:hover {
            background: #f8f9fa;
        }

        .item-checkbox {
            width: 18px;
            height: 18px;
            accent-color: var(--primary-color);
            cursor: pointer;
        }

        .product-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .product-info img {
            width: 80px;
            height: 80px;
            object-fit: cover;
            border-radius: 12px;
            border: 2px solid #f0f0f0;
            transition: all 0.3s;
        }

        .product-info img:hover {
            border-color: var(--primary-color);
            transform: scale(1.05);
        }

        .product-name {
            font-weight: 600;
            color: var(--text-primary);
            text-decoration: none;
            font-size: 16px;
            line-height: 1.4;
        }

        .product-name:hover {
            color: var(--primary-color);
        }

        .price-cell {
            font-weight: 600;
            color: var(--primary-color);
            font-size: 15px;
        }

        .cart-plus-minus {
            position: relative;
            display: flex;
            align-items: center;
            background: #f8f9fa;
            border-radius: 8px;
            padding: 5px;
            border: 2px solid #e1e5e9;
            transition: border-color 0.3s;
        }

        .cart-plus-minus:focus-within {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(139, 158, 107, 0.1);
        }

        .weight-input {
            width: 80px;
            text-align: center;
            padding: 8px 25px 8px 8px;
            border: none;
            background: transparent;
            font-size: 14px;
            font-weight: 600;
            color: var(--text-primary);
        }

        .weight-input:focus {
            outline: none;
        }

        .weight-unit {
            position: absolute;
            right: 8px;
            color: var(--text-secondary);
            font-size: 12px;
            pointer-events: none;
            font-weight: 500;
        }

        .subtotal-cell {
            font-weight: 700;
            color: var(--primary-color);
            font-size: 18px;
        }

        .remove-btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 36px;
            height: 36px;
            background: #fff5f5;
            color: #dc3545;
            border-radius: 50%;
            text-decoration: none;
            transition: all 0.3s;
            border: 2px solid transparent;
        }

        .remove-btn:hover {
            background: #dc3545;
            color: white;
            transform: scale(1.1);
            border-color: #dc3545;
        }

        .sidebar-title {
            font-size: 1.3rem;
            font-weight: 600;
            color: var(--text-primary);
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid var(--primary-color);
            display: flex;
            align-items: center;
        }

        .sidebar-title i {
            margin-right: 10px;
            color: var(--primary-color);
            font-size: 1.2rem;
        }

        .cart-summary {
            margin-bottom: 25px;
        }

        .summary-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            padding: 12px 0;
            border-bottom: 1px solid #f0f0f0;
        }

        .summary-row:last-child {
            margin-bottom: 0;
            padding-top: 20px;
            border-top: 2px solid var(--primary-color);
            border-bottom: none;
            font-weight: 700;
            font-size: 18px;
        }

        .summary-label {
            color: var(--text-regular);
            font-weight: 500;
        }

        .summary-value {
            color: var(--text-primary);
            font-weight: 600;
        }

        .total-amount {
            color: var(--primary-color);
            font-size: 22px;
            font-weight: 700;
        }

        .cart-actions {
            display: flex;
            flex-direction: column;
            gap: 12px;
        }

        .action-group {
            display: flex;
            gap: 10px;
        }

        .btn-custom {
            padding: 12px 20px;
            border: none;
            border-radius: 8px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            font-size: 14px;
            flex: 1;
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--primary-color), #7a8d5d);
            color: white;
            border: 2px solid transparent;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(139, 158, 107, 0.4);
            color: white;
        }

        .btn-secondary {
            background: white;
            color: var(--text-regular);
            border: 2px solid #e1e5e9;
        }

        .btn-secondary:hover {
            background: #f8f9fa;
            border-color: var(--primary-color);
            color: var(--primary-color);
        }

        .btn-danger {
            background: white;
            color: #dc3545;
            border: 2px solid #dc3545;
        }

        .btn-danger:hover {
            background: #dc3545;
            color: white;
            transform: translateY(-1px);
        }

        .btn-large {
            padding: 15px 25px;
            font-size: 16px;
            font-weight: 600;
        }

        .empty-cart {
            text-align: center;
            padding: 80px 20px;
            color: var(--text-secondary);
            background: white;
            border-radius: 12px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.08);
        }

        .empty-cart i {
            font-size: 5rem;
            margin-bottom: 25px;
            opacity: 0.3;
            color: var(--primary-color);
        }

        .empty-cart h3 {
            font-size: 1.8rem;
            margin-bottom: 15px;
            color: var(--text-primary);
        }

        .empty-cart p {
            font-size: 1.1rem;
            margin-bottom: 30px;
            color: var(--text-regular);
        }

        /* 响应式设计 */
        @media (max-width: 1200px) {
            .cart-content {
                grid-template-columns: 1fr;
                gap: 20px;
            }

            .cart-sidebar {
                position: static;
                order: -1;
            }
        }

        @media (max-width: 768px) {
            .cart-container {
                padding: 15px;
            }

            .cart-header {
                padding: 30px 20px;
            }

            .cart-header h1 {
                font-size: 2rem;
            }

            .cart-main,
            .cart-sidebar {
                padding: 20px;
            }

            .cart-table th,
            .cart-table td {
                padding: 15px 8px;
                font-size: 14px;
            }

            .product-info {
                flex-direction: column;
                gap: 10px;
                text-align: center;
            }

            .product-info img {
                width: 60px;
                height: 60px;
            }

            .cart-actions {
                gap: 10px;
            }

            .action-group {
                flex-direction: column;
            }

            .btn-custom {
                padding: 12px 16px;
                font-size: 13px;
            }
        }

        @media (max-width: 480px) {
            .cart-table {
                font-size: 12px;
            }

            .cart-table th,
            .cart-table td {
                padding: 10px 5px;
            }

            .weight-input {
                width: 60px;
                font-size: 12px;
            }

            .product-name {
                font-size: 14px;
            }

            .price-cell,
            .subtotal-cell {
                font-size: 13px;
            }
        }
    </style>
</head>
<body>

<div class="wrapper">
    <header>
        <div class="header-area bg-gray">
            <div class="container">
                <div class="row">
                    <div class="col-lg-3 col-md-4">
                        <div class="logo">
                            <a href="<%=basePath%>views/user/index.jsp"
                            ><img src="<%=basePath%>images/img/logo/logo.png" alt=""
                            /></a>
                        </div>
                    </div>
                    <div class="col-lg-9 col-md-8">
                        <div class="header-main-content">
                            <div class="main-menu-area d-none d-lg-block">
                                <nav>
                                    <ul>
                                        <li>
                                            <a href="<%=basePath%>views/user/index.jsp"
                                            >首页
                                                <%--                            <i class="icofont icofont-simple-down"></i>--%>
                                            </a>
                                            <%--
                                            <ul class="sub-menu">
                                              --%> <%--
                            <li><a href="home-box.html">首页盒</a></li>
                            --%> <%--
                          </ul>
                          --%>
                                        </li>
                                        <li>
                                            <a href="<%=basePath%>views/user/about-us.jsp"
                                            >关于我们</a
                                            >
                                        </li>
                                        <li>
                                            <a href="<%=basePath%>user/shop"
                                            >商城 <i class="icofont icofont-simple-down"></i
                                            ></a>
                                            <ul class="sub-menu">
                                                <%--
                                                <li><a href="shop-right.html">正确选购</a></li>
                                                --%>
                                                <li>
                                                    <a href="<%=basePath%>user/cart"
                                                    >购物车</a
                                                    >
                                                </li>
                                                <li>
                                                    <a href="<%=basePath%>user/orders"
                                                    >订单</a
                                                    >
                                                </li>
                                                <%--
                                                <li>
                                                  <a href="<%=basePath%>views/user/wishlist.jsp"
                                                    >愿望清单</a
                                                  >
                                                </li>
                                                --%>
                                            </ul>
                                        </li>
                                        <li>
                                            <a href="<%=basePath%>views/user/shop.jsp">
                                                我的 <i class="icofont icofont-simple-down"></i
                                            ></a>
                                            <ul class="sub-menu">
                                                <li>
                                                    <a href="<%=basePath%>views/user/my-account.jsp"
                                                    >我的账户</a
                                                    >
                                                </li>
                                                <%--
                                                <li>
                                                  <a
                                                    href="<%=basePath%>views/user/single-product.jsp"
                                                    >单品</a
                                                  >
                                                </li>
                                                --%> <%--
                            <li>
                              <a href="<%=basePath%>views/user/blog-details.jsp"
                                >博客详情</a
                              >
                            </li>
                            --%>
                                                <li>
                                                    <a href="<%=basePath%>views/login.jsp"
                                                    >登录注册</a
                                                    >
                                                </li>
                                            </ul>
                                        </li>
                                        <li>
                                            <a href="<%=basePath%>views/user/blog.jsp">博客</a>
                                        </li>
                                        <li>
                                            <a href="<%=basePath%>views/user/contact-us.jsp">
                                                联系我们</a
                                            >
                                        </li>
                                    </ul>
                                </nav>
                            </div>
                            <div class="right-blok-box">
                                <div class="serch-container hidden-xs">
                                    <a href="#"><i class="icon-magnifier"></i></a>
                                    <div class="blog-search">
                                        <form action="#" id="search">
                                            <input
                                                    type="text"
                                                    placeholder="点击此处进行搜索"
                                                    name="s"
                                            />
                                            <button type="submit">
                                                <i class="icofont icofont-search-alt-1"></i>
                                            </button>
                                        </form>
                                    </div>
                                </div>
                                <div class="mine-cart-box">
                                    <a href="<%=basePath%>user/cart"
                                    ><i class="icon-handbag"></i
                                    ><span id="cart-total">2</span></a
                                    >
                                    <ul class="mini-cart">
                                        <li class="cart-item">
                                            <div class="cart-image">
                                                <a href="<%=basePath%>views/user/single-product.jsp"
                                                ><img
                                                        src="<%=basePath%>images/img/product/1.jpg"
                                                        alt=""
                                                /></a>
                                            </div>
                                            <div class="cart-title">
                                                <a href="<%=basePath%>views/user/single-product.jsp"
                                                ><h4>热茶</h4></a
                                                >
                                                <span class="quantity">1 ×</span>
                                                <div class="price-box">
                                                    <span class="new-price">$ 130.00</span>
                                                </div>
                                                <a href="#" class="remove_from_cart"
                                                ><i class="icon-trash icons"></i
                                                ></a>
                                            </div>
                                        </li>
                                        <li class="cart-item">
                                            <div class="cart-image">
                                                <a href="<%=basePath%>views/user/single-product.jsp"
                                                ><img
                                                        src="<%=basePath%>images/img/product/6.jpg"
                                                        alt=""
                                                /></a>
                                            </div>
                                            <div class="cart-title">
                                                <a href="<%=basePath%>views/user/single-product.jsp"
                                                ><h4>水果绿茶</h4></a
                                                >
                                                <span class="quantity">1 ×</span>
                                                <div class="price-box">
                                                    <span class="new-price">$ 100.99</span>
                                                </div>
                                                <a href="#" class="remove_from_cart"
                                                ><i class="icon-trash icons"></i
                                                ></a>
                                            </div>
                                        </li>
                                        <li class="subtotal-titles">
                                            <div class="subtotal-titles">
                                                <h3>总价 :</h3>
                                                <span>$ 230.99</span>
                                            </div>
                                        </li>
                                        <li class="mini-cart-btns">
                                            <div class="cart-btns">
                                                <a href="<%=basePath%>user/cart"
                                                >购物车</a
                                                >
                                                <a href="<%=basePath%>user/orders">订单</a>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                                <div class="top-login-menu">
                                    <div class="top-login-inner">
                                        <c:choose>
                                            <c:when test="${not empty sessionScope.customerName}">
                                                <a href="<%=basePath%>views/user/my-account.jsp"
                                                ><i class="icon-user"></i>
                                                    <span class="user-login"
                                                    >${sessionScope.customerName}</span
                                                    >
                                                </a>
                                            </c:when>
                                            <c:otherwise>
                                                <a href="<%=basePath%>views/login.jsp"
                                                ><i class="icon-user"></i>
                                                    <span class="user-login">Login</span>
                                                </a>
                                            </c:otherwise>
                                        </c:choose>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12">
                        <!-- Mobile Menu Area Start -->
                        <div class="mobile-menu-area d-block d-lg-none">
                            <div class="mobile-menu">
                                <nav id="mobile-menu-active">
                                    <ul>
                                        <li>
                                            <a href="<%=basePath%>views/user/index.jsp">首页</a>
                                            <%--
                                            <ul>
                                              --%> <%--
                            <li><a href="home-box.html">首页盒</a></li>
                            --%> <%--
                          </ul>
                          --%>
                                        </li>
                                        <li>
                                            <a href="<%=basePath%>views/user/about-us.jsp"
                                            >关于我们</a
                                            >
                                        </li>
                                        <li>
                                            <a href="<%=basePath%>views/user/shop.jsp">商城</a>
                                            <ul>
                                                <li><a href="shop-right.html">Shop Right</a></li>
                                                <li>
                                                    <a href="<%=basePath%>views/user/checkout.jsp"
                                                    >Checkout</a
                                                    >
                                                </li>
                                                <li>
                                                    <a href="<%=basePath%>views/user/wishlist.jsp"
                                                    >Wish List</a
                                                    >
                                                </li>
                                                <li>
                                                    <a href="<%=basePath%>views/user/cart.jsp"
                                                    >Cart</a
                                                    >
                                                </li>
                                            </ul>
                                        </li>
                                        <li>
                                            <a href="#"> Pages</a>
                                            <ul>
                                                <li>
                                                    <a href="<%=basePath%>views/user/my-account.jsp"
                                                    >My account</a
                                                    >
                                                </li>
                                                <li>
                                                    <a
                                                            href="<%=basePath%>views/user/single-product.jsp"
                                                    >Single Product</a
                                                    >
                                                </li>
                                                <li>
                                                    <a href="<%=basePath%>views/user/blog-details.jsp"
                                                    >Blog details</a
                                                    >
                                                </li>
                                                <li>
                                                    <a href="<%=basePath%>views/login.jsp"
                                                    >Login Register</a
                                                    >
                                                </li>
                                            </ul>
                                        </li>
                                        <li>
                                            <a href="<%=basePath%>views/user/blog.jsp">Blog</a>
                                        </li>
                                        <li>
                                            <a href="<%=basePath%>views/user/contact-us.jsp">
                                                Contact Us</a
                                            >
                                        </li>
                                    </ul>
                                </nav>
                            </div>
                        </div>
                        <!-- Mobile Menu Area End -->
                    </div>
                </div>
            </div>
        </div>
    </header>
    <!-- 购物车头部 -->
    <div class="cart-header">
        <h1><i class="fas fa-shopping-cart" style="margin-right: 10px;"></i> 购物车</h1>
        <p>管理您的茶叶商品</p>
    </div>

    <!-- 购物车内容 -->
    <div class="cart-content">
        <c:choose>
            <c:when test="${not empty cartItems}">
                <!-- 购物车主要内容 -->
                <div class="cart-main">
                    <div class="table-responsive">
                        <table class="cart-table">
                            <thead>
                                <tr>
                                    <th class="product-thumbnail">选择</th>
                                    <th class="cart-product-name">商品</th>
                                    <th class="product-price">单价</th>
                                    <th class="product-quantity">重量</th>
                                    <th class="product-subtotal">小计</th>
                                    <th class="product-remove">操作</th>
                                </tr>
                            </thead>
                            <tbody id="cart-items">
                                <c:forEach items="${cartItems}" var="item" varStatus="status">
                                    <tr data-item-id="${item.id}">
                                        <td class="product-thumbnail">
                                            <input type="checkbox" class="item-checkbox"
                                                   ${item.selected == 1 ? 'checked' : ''}
                                                   onchange="updateItemSelection(${item.id}, this.checked)">
                                        </td>
                                        <td class="cart-product-name">
                                            <div class="product-info">
                                                <img src="<%=basePath%>images/img/product/${item.tea.image != null ? item.tea.image : '1.jpg'}" alt="${item.tea.tea_name}">
                                                <a href="<%=basePath%>user/product?id=${item.tea_id}" class="product-name">
                                                    ${item.tea.tea_name}
                                                </a>
                                            </div>
                                        </td>
                                        <td class="product-price">
                                            <span class="price-cell">¥<fmt:formatNumber value="${item.price}" pattern="#,##0.00"/>/${item.tea.unit}</span>
                                        </td>
                                        <td class="product-quantity">
                                            <div class="cart-plus-minus">
                                                <input type="number" class="cart-plus-minus-box weight-input"
                                                       value="${item.quantity * (item.tea.unit == '50g' ? 50 : (item.tea.unit == '357g' ? 357 : (item.tea.unit == '250g' ? 250 : 100)))}"
                                                       min="1" step="1"
                                                       data-item-id="${item.id}"
                                                       data-unit="${item.tea.unit}"
                                                       data-price="${item.price}"
                                                       data-quantity="${item.quantity}"
                                                       onchange="updateItemWeight(this)">
                                                <span class="weight-unit">g</span>
                                            </div>
                                        </td>
                                        <td class="product-subtotal">
                                            <span class="subtotal-cell subtotal-${item.id}">
                                                ¥<fmt:formatNumber value="${item.quantity * item.price}" pattern="#,##0.00"/>
                                            </span>
                                        </td>
                                        <td class="product-remove">
                                            <a href="#" class="remove-btn" onclick="removeItem(${item.id})" title="删除商品">
                                                <i class="fas fa-trash"></i>
                                            </a>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </tbody>
                        </table>
                    </div>
                </div>

                <!-- 购物车侧边栏 -->
                <div class="cart-sidebar">
                    <div class="sidebar-title">
                        <i class="fas fa-calculator"></i>
                        购物车汇总
                    </div>

                    <div class="cart-summary">
                        <div class="summary-row">
                            <span class="summary-label">商品总计：</span>
                            <span class="summary-value" id="cart-subtotal">¥<fmt:formatNumber value="${totalAmount}" pattern="#,##0.00"/></span>
                        </div>
                        <div class="summary-row">
                            <span class="summary-label">运费：</span>
                            <span class="summary-value">免费</span>
                        </div>
                        <div class="summary-row">
                            <span class="summary-label">总计：</span>
                            <span class="total-amount" id="cart-total-amount">¥<fmt:formatNumber value="${totalAmount}" pattern="#,##0.00"/></span>
                        </div>
                    </div>

                    <div class="cart-actions">
                        <button type="button" class="btn-custom btn-primary btn-large" onclick="checkout()">
                            <i class="fas fa-credit-card"></i> 立即结算
                        </button>

                        <div class="action-group">
                            <button type="button" class="btn-custom btn-secondary" onclick="selectAll()">
                                <i class="fas fa-check-square"></i> 全选
                            </button>
                            <button type="button" class="btn-custom btn-danger" onclick="clearSelected()">
                                <i class="fas fa-trash"></i> 清空选中
                            </button>
                        </div>

                        <a href="<%=basePath%>user/shop" class="btn-custom btn-secondary">
                            <i class="fas fa-arrow-left"></i> 继续购物
                        </a>
                    </div>
                </div>
            </c:when>
            <c:otherwise>
                <div class="empty-cart">
                    <i class="fas fa-shopping-cart"></i>
                    <h3>购物车是空的</h3>
                    <p>您还没有添加任何商品到购物车</p>
                    <a href="<%=basePath%>user/shop" class="btn-custom btn-checkout">
                        <i class="fas fa-shopping-bag"></i> 去购物
                    </a>
                </div>
            </c:otherwise>
        </c:choose>
    </div>
</div>
    <!-- Header End -->

    <!-- Breadcrumb Area Start -->
<%--    <div class="breadcrumb-area ptb-100 bg-gray">--%>
<%--        <div class="container">--%>
<%--            <div class="row">--%>
<%--                <div class="col">--%>
<%--                    <div class="breadcrumb-content text-center">--%>
<%--                        <h2>购物车</h2>--%>
<%--                        <ul>--%>
<%--                            <li><a href="<%=basePath%>views/user/index.jsp">首页</a></li>--%>
<%--                            <li><a href="<%=basePath%>user/shop">商城</a></li>--%>
<%--                            <li class="active">购物车</li>--%>
<%--                        </ul>--%>
<%--                    </div>--%>
<%--                </div>--%>
<%--            </div>--%>
<%--        </div>--%>
<%--    </div>--%>
    <!-- Breadcrumb Area End -->

<!-- All JS Hear -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

<script>
// 更新商品选择状态
function updateItemSelection(itemId, selected) {
    $.ajax({
        url: '<%=basePath%>user/updateCart',
        type: 'POST',
        data: {
            itemId: itemId,
            selected: selected ? 1 : 0
        },
        success: function(response) {
            if (response.success) {
                calculateTotal();
            } else {
                alert(response.message || '更新失败');
            }
        },
        error: function() {
            alert('网络错误，请重试');
        }
    });
}

// 更新商品重量
function updateItemWeight(inputElement) {
    var weight = parseFloat(inputElement.value);
    var itemId = inputElement.getAttribute('data-item-id');
    var unit = inputElement.getAttribute('data-unit');
    var price = parseFloat(inputElement.getAttribute('data-price'));

    if (weight <= 0) {
        alert('重量必须大于0');
        inputElement.value = 1;
        return;
    }

    // 提取单位重量（如"50g"中的50）
    var unitWeight = parseFloat(unit.replace(/[^0-9.]/g, ''));

    // 计算数量：重量 / 单位重量
    var quantity = weight / unitWeight;

    // 计算小计：数量 * 单价
    var subtotal = quantity * price;

    // 更新显示
    var row = $(inputElement).closest('tr');
    row.find('.subtotal-' + itemId).text('¥' + subtotal.toFixed(2));

    // 发送到后端更新
    $.ajax({
        url: '<%=basePath%>user/updateCart',
        type: 'POST',
        data: {
            itemId: itemId,
            quantity: quantity
        },
        success: function(response) {
            if (response.success) {
                calculateTotal();
            } else {
                alert(response.message || '更新失败');
                // 恢复原值
                inputElement.value = unitWeight;
            }
        },
        error: function() {
            alert('网络错误，请重试');
            // 恢复原值
            inputElement.value = unitWeight;
        }
    });
}

// 移除商品
function removeItem(itemId) {
    if (confirm('确定要删除这个商品吗？')) {
        $.ajax({
            url: '<%=basePath%>user/removeFromCart',
            type: 'POST',
            data: {
                itemId: itemId
            },
            success: function(response) {
                if (response.success) {
                    $('tr[data-item-id="' + itemId + '"]').remove();
                    calculateTotal();
                    updateCartCount();
                } else {
                    alert(response.message || '删除失败');
                }
            },
            error: function() {
                alert('网络错误，请重试');
            }
        });
    }
}

// 全选
function selectAll() {
    $('.item-checkbox').prop('checked', true);
    $('.item-checkbox').each(function() {
        var itemId = $(this).closest('tr').data('item-id');
        updateItemSelection(itemId, true);
    });
}

// 清空选中
function clearSelected() {
    if (confirm('确定要清空所有选中的商品吗？')) {
        $('.item-checkbox:checked').each(function() {
            var itemId = $(this).closest('tr').data('item-id');
            removeItem(itemId);
        });
    }
}

// 计算总计
function calculateTotal() {
    var total = 0;
    var selectedTotal = 0;

    // 计算所有商品的总价（用于显示）
    $('.weight-input').each(function() {
        var weight = parseFloat($(this).val());
        var unit = $(this).attr('data-unit');
        var price = parseFloat($(this).attr('data-price'));

        // 提取单位重量（如"50g"中的50）
        var unitWeight = parseFloat(unit.replace(/[^0-9.]/g, ''));
        console.log(unitWeight);

        // 计算数量：重量 / 单位重量
        var quantity = weight / unitWeight;
        console.log(quantity);

        // 计算小计：数量 * 单价
        var subtotal = quantity * price;
        console.log(subtotal);
        total += subtotal;
        console.log(total);
    });

    // 计算选中商品的总价（用于结算）
    $('.item-checkbox:checked').each(function() {
        var row = $(this).closest('tr');
        var weightInput = row.find('.cart-plus-minus-box');
        var weight = parseFloat(weightInput.val());
        var unit = weightInput.attr('data-unit');
        var price = parseFloat(weightInput.attr('data-price'));

        // 提取单位重量（如"50g"中的50）
        var unitWeight = parseFloat(unit.replace(/[^0-9.]/g, ''));

        // 计算数量：重量 / 单位重量
        var quantity = weight / unitWeight;

        // 计算小计：数量 * 单价
        var subtotal = quantity * price;
        selectedTotal += subtotal;
    });

    // 显示选中商品的总价，如果没有选中则显示所有商品总价
    var displayTotal = $('.item-checkbox:checked').length > 0 ? selectedTotal : total;
    $('#cart-subtotal').text('¥' + displayTotal.toFixed(2));
    $('#cart-total-amount').text('¥' + displayTotal.toFixed(2));
}

// 结算
function checkout() {
    var selectedItems = $('.item-checkbox:checked').length;
    if (selectedItems === 0) {
        alert('请选择要结算的商品');
        return;
    }
    
    if (confirm('确定要结算选中的商品吗？')) {
        $.ajax({
            url: '<%=basePath%>user/orders',
            type: 'POST',
            success: function(response) {
                if (response.success) {
                    alert('订单创建成功！订单号：' + response.orderNo);
                } else {
                    alert(response.message || '结算失败');
                }
            },
            error: function() {
                alert('网络错误，请重试');
            }
        });
    }
}

// 更新购物车数量
function updateCartCount() {
    var count = $('#cart-items tr').length;
    $('#cart-total').text(count);
}

// 页面加载完成后的初始化
$(document).ready(function() {
    // 初始化每个商品的重量和小计显示
    $('.weight-input').each(function() {
        var unit = $(this).attr('data-unit');
        var price = parseFloat($(this).attr('data-price'));
        var quantity = parseFloat($(this).attr('data-quantity'));
        var itemId = $(this).attr('data-item-id');

        // 提取单位重量
        var unitWeight = parseFloat(unit.replace(/[^0-9.]/g, ''));

        // 计算实际重量：数量 * 单位重量
        var actualWeight = quantity * unitWeight;

        // 设置重量输入框的值
        $(this).val(actualWeight);

        // 计算小计
        var subtotal = quantity * price;

        // 更新小计显示
        $('.subtotal-' + itemId).text('¥' + subtotal.toFixed(2));
    });

    calculateTotal();
});
</script>

</body>
</html>
